/**
 * Copyright 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.domesticmouse.waveoteditor.client.wave.model.id;

/**
 * A globally-unique wavelet identifier.
 *
 *
 */
public class WaveletName implements Comparable<WaveletName> {
  public final WaveId waveId;
  public final WaveletId waveletId;

  /** Constructs a wavelet name for a wave id and wavelet id. */
  public static WaveletName of(WaveId waveId, WaveletId waveletId) {
    return new WaveletName(waveId, waveletId);
  }

  /** Constructs a wavelet name for a serialised wave id and wavelet id. */
  public static WaveletName of(String waveId, String waveletId) {
    return new WaveletName(WaveId.deserialise(waveId), WaveletId.deserialise(waveletId));
  }

  /** Private constructor to allow future instance optimisation. */
  private WaveletName(WaveId waveId, WaveletId waveletId) {
    this.waveId = waveId;
    this.waveletId = waveletId;
  }

  @Override
  public String toString() {
    return waveId + "/" + waveletId;
  }

  @Override
  public boolean equals(Object other) {
    if (other instanceof WaveletName) {
      WaveletName o = (WaveletName) other;
      return waveId.equals(o.waveId) && waveletId.equals(o.waveletId);
    }
    return false;
  }

  @Override
  public int hashCode() {
    return waveId.hashCode() * 37 + waveletId.hashCode();
  }

  @Override
  public int compareTo(WaveletName o) {
    return waveId.equals(o.waveId) ? waveletId.compareTo(o.waveletId)
                                   : waveId.compareTo(o.waveId);
  }
}
